$(function(){
	var backViewType = $('#backViewType').val();
	if(backViewType == 2){ // 管局可修改
		$('#btn_10').show();
		$("#queryForm").find('input[isshow]').each(function(){
			$(this).removeAttr("readonly");
		})
	}
	
    var singleTaskId = $('#singleTaskId').val();
	getInfoBySingleTaskId(singleTaskId);
})

/**
 * 获取表单详情
 * @param singleTaskId
 */
function getInfoBySingleTaskId(singleTaskId){
	var eType = $('#eType').val();
	var userId = $('#userId').val();
	var backViewType = $('#backViewType').val();
	$.ajax({
		type : "POST",
		url : "reportMol/getMolData",
		data : {
			'singleTaskId':singleTaskId, 
			'eType': eType, 
			'userId': userId,
			'backViewType': backViewType,
			'statue': $('#statue').val(),
			'type': $('#type').val()
		},
		traditional: true,
		dataType : "json",
		success : function(data) {
			if (data.flag) {
				fillFormData('#queryForm', data.data);
				var houseData = data.houseData;
				if(null != houseData){
					setHouseBody(houseData);
				}
				var gbpsData = data.gbpsData;
				if(null != gbpsData){
					setGbpsData(gbpsData);
				}
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}

function setHouseBody(data){
	if(data && data.length > 0){
		var houseNum = data.length;
		$('input[name="houseNum"]').val(houseNum);
		
		var houseHtml = ''; // 节点机房信息
		var backViewType = $('#backViewType').val();
		for(var i=0; i<data.length; i++){
			var nodeName = 'houseList[' + i + '].nodeName';
			var houseAddr = 'houseList[' + i + '].houseAddr';
			var singleTaskId = 'houseList[' + i + '].singleTaskId';
			
			if(backViewType == 2){
				houseHtml += '<tr>'
					+ '<td>'
					+ '<span>'+ (i+1) +')</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+nodeName+'" value="'+data[i].nodeName+'"/>'
					+ '<input type="text" class="form-control input-sm" name="'+singleTaskId+'" value="'+data[i].singleTaskId+'" style="display:none;"/>'
					+ '<span>：</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+houseAddr+'" value="'+data[i].houseAddr+'"/>'
					+ '</td>'
					+ '</tr>';
			} else {
				houseHtml += '<tr>'
					+ '<td>'
					+ '<span>'+ (i+1) +')</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+nodeName+'" value="'+data[i].nodeName+'" readonly/>'
					+ '<input type="text" class="form-control input-sm" name="'+singleTaskId+'" value="'+data[i].singleTaskId+'" style="display:none;"/>'
					+ '<span>：</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+houseAddr+'" value="'+data[i].houseAddr+'" readonly/>'
					+ '</td>'
					+ '</tr>';
			}
			
		}
		$('#node_house_tbody').html(houseHtml);
	}
}

function setGbpsData(data){
	if(data && data.length > 0){
		var html = ''; // 节点部署省份
		var backViewType = $('#backViewType').val();
		for(var i=0; i<data.length; i++){
			var province = 'gbpsList[' + i + '].province';
			var type = 'gbpsList[' + i + '].type';
			var gbps = 'gbpsList[' + i + '].gbps';
			var singleTaskId = 'gbpsList[' + i + '].singleTaskId';
			
			if(backViewType == 2){
				html += '<tr>'
					+ '<td>'
					+ '<span>'+ (i+1) +')</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+province+'" value="'+data[i].province+'"/>'
					+ '<input type="text" class="form-control input-sm" name="'+singleTaskId+'" value="'+data[i].singleTaskId+'" style="display:none;"/>'
					+ '<span>：</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+type+'" value="'+data[i].type+'"/>'
					+ '<span>，</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+gbps+'" value="'+data[i].gbps+'"/>'
					+ '</td>'
					+ '</tr>';
			} else {
				html += '<tr>'
					+ '<td>'
					+ '<span>'+ (i+1) +')</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+province+'" value="'+data[i].province+'" readonly/>'
					+ '<input type="text" class="form-control input-sm" name="'+singleTaskId+'" value="'+data[i].singleTaskId+'" style="display:none;"/>'
					+ '<span>：</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+type+'" value="'+data[i].type+'" readonly/>'
					+ '<span>，</span>'
					+ '</td>'
					+ '<td>'
					+ '<input type="text" class="form-control input-sm" name="'+gbps+'" value="'+data[i].gbps+'" readonly/>'
					+ '</td>'
					+ '</tr>';
			}
			
		}
		$('#node_provin_tbody').html(html);
	}
}

/**
 * form 数据填充
 */
function fillFormData(form, obj) {
	var formEL = $(form);
	$.each(obj, function(index, item) {
	   formEL.find("[name=" + index + "]").val(item);
	});
}

/**
 * 返回按钮
 * @returns
 */
function backView(){
	var backViewType = $('#backViewType').val();
	var userId = $('#userId').val();
	if(backViewType == 1){ // 企业侧
		window.location.replace("singleTask?userId="+userId);
	} else if(backViewType == 2){ //管局侧
		window.location.replace("evalTaskAudit");
	}
}

function updateMolData(){
	$.ajax({
		type : "POST",
		url : "reportMol/upOnlyIntMolData",
		data : $("#queryForm").serialize(),
		traditional: true,
		dataType : "json",
		success : function(data) {
			if (data.flag) {
				layer.alert("修改成功！", {icon : 3,offset: '100px'});
			} else {
				layer.alert("修改失败！", {icon : 3,offset: '100px'});
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}